package org.assistant.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat sdf2 = new SimpleDateFormat(
		"yyyy-MM-dd HH:mm:ss");

	/**
	 * 将date转换为字符串（yyyy-MM-dd）
	 * 
	 * @param date
	 * @return
	 */
	public static String toString(Date date) {
		if (date == null) {
			return "";
		}
		return sdf.format(date);
	}

	/**
	 * 将date转换为字符串（yyyy-MM-dd HH:mm:ss）
	 * 
	 * @param date
	 * @return
	 */
	public static String toString2(Date date) {
		if (date == null) {
			return "";
		}
		return sdf2.format(date);
	}

	/**
	 * 将字符串date解析为Date类型的对象
	 * 
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String dateStr) throws ParseException {
		if (dateStr == null) {
			throw new IllegalArgumentException("Date String must not be null");
		}
		int length = dateStr.length();
		if (length == 10) {
			return sdf.parse(dateStr);
		} else if (length == 19) {
			return sdf2.parse(dateStr);
		} else {
			throw new IllegalArgumentException(
				"Date String has incorrect format");
		}
	}
}
